package com.hw.hcit.feature;

import java.io.FileOutputStream;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.SystemProperties;
import android.provider.Settings;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;



public class IndicatorLightTest extends JTestItem implements OnClickListener {
   private static final String TAG = "IndicatorLightTest";
   private Context mContext;
   private final int RED = 0;
   private final int GREEN = 1;
   private final int INIT_COLOR_NUM = 2;
   private int color = RED;
   final byte[] LIGHT_ON = { '2', '5', '5' };
   final byte[] LIGHT_OFF = { '0' };
   String RED_LED_DEV = "/sys/class/leds/red/brightness";
   String GREEN_LED_DEV = "/sys/class/leds/green/brightness";
   private final int FLASH_RED_LIGHT = 101;
   private final int FLASH_GREEN_LIGHT = 102;
   private final int FLASH_INTERVAL = 500;
   private Button mButton1, mButton2;
   private Intent intent;
   private static int brightness;
   private int colorNum = INIT_COLOR_NUM;

   public IndicatorLightTest(JCtrlCallback cb) {
      super(cb);
   }

   public IndicatorLightTest() {
      super();
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.indicator_light_test,
            container, false);
      mButton1 = (Button) rootView.findViewById(R.id.button1);
      mButton2 = (Button) rootView.findViewById(R.id.button2);
      mButton1.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (Config.CURR_PROJECT.isAuto()) {
               if (!Config.ageingTested) {
                  rewriteOneByteOfNv2499(7, 'P');
               } else {
                  rewriteOneByteOfNv2499(38, 'P');
               }
            }
            testFinish(true, 1, true);

         }
      });
      mButton2.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (Config.CURR_PROJECT.isAuto()) {
               if (!Config.ageingTested) {
                  rewriteOneByteOfNv2499(7, 'F');
               } else {
                  rewriteOneByteOfNv2499(38, 'F');
               }
            }
            testFinish(false, 1, false);

         }
      });
      return rootView;
   }

   @Override
   public void onAttach(Activity activity) {
      // TODO Auto-generated method stub
      super.onAttach(activity);
      mContext = activity;

   }

   @Override
   public void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      mHandler.sendEmptyMessage(FLASH_RED_LIGHT);
   }

   @Override
   public void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      Log.d(TAG, "----------->++onPause+");
      mHandler.removeMessages(FLASH_GREEN_LIGHT);
      mHandler.removeMessages(FLASH_RED_LIGHT);
      setColor(-1);
   }

   @Override
   public void onClick(View v) {
      // TODO Auto-generated method stub
   }

   Handler mHandler = new Handler() {

      public void handleMessage(Message msg) {
         super.handleMessage(msg);
         switch (msg.what) {
         case FLASH_RED_LIGHT:
            setColor(RED);
            mHandler.sendEmptyMessageDelayed(FLASH_GREEN_LIGHT,
                  FLASH_INTERVAL);
            break;
         case FLASH_GREEN_LIGHT:
            setColor(GREEN);
            mHandler.sendEmptyMessageDelayed(FLASH_RED_LIGHT,
                  FLASH_INTERVAL);
            break;
         default:
            break;
         }
      }
   };

   private void setColor(int color) {

      logd("set:" + color);
      boolean red = false, green = false;
      switch (color) {
      case RED:
         red = true;
         break;
      case GREEN:
         green = true;
         break;
      default:
         break;
      }
      try {
         FileOutputStream fRed = new FileOutputStream(RED_LED_DEV);
         fRed.write(red ? LIGHT_ON : LIGHT_OFF);
         fRed.close();
         FileOutputStream fGreen = new FileOutputStream(GREEN_LED_DEV);
         fGreen.write(green ? LIGHT_ON : LIGHT_OFF);
         fGreen.close();
      } catch (Exception e) {
         loge(e);
      }

   }

   void logd(Object d) {

      Log.d(TAG, "" + d);
   }

   void loge(Object e) {

      Log.e(TAG, "" + e);
   }

}
